package myjava.i_collection;

import java.util.Set;
import java.util.TreeSet;

/**
 * @Auther: cty
 * @Date: 2019/11/15 下午10:45
 * @Description: 测试TreeSet
 * @version: 1.0
 */
public class L_TestTreeSet {
    public static void main(String[] args) {
        Set<Integer> set = new TreeSet<>();
        set.add(300);
        set.add(200);
        set.add(600);
        //按照元素递增的方式排序
        for(Integer s:set){
            System.out.println(s);
        }

        System.out.println("###########################################");

        Set<Emp2> set2 = new TreeSet<>();
        set2.add(new Emp2(100,"张三",50000));
        set2.add(new Emp2(200,"李四",6000));
        set2.add(new Emp2(150,"王五",5000));
        set2.add(new Emp2(170,"赵六",6000));

        for(Emp2 s:set2){
            System.out.println(s);
        }
    }
}

class Emp2 implements Comparable<Emp2>{
    int id;
    String name;
    double salary;

    public Emp2(int id, String name, double salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    @Override
    public int compareTo(Emp2 o) {
        if(this.salary > o.salary){
            return 1;
        }else if(this.salary < o.salary){
            return -1;
        }else{
            if(this.id > o.id){
                return 1;
            }else if(this.id < o.id){
                return -1;
            }else {
                return 0;
            }
        }
    }

    @Override
    public String toString() {
        return "Emp2{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", salary=" + salary +
                '}';
    }
}
